#include "tc.h"

using namespace std;

int maxBandwidth = 1000;
int minBandwidth = 0;
string netcard = "eth1";

void myTrafficControl()
{
	string command = "iptables -P INPUT ACCEPT";
	system(command.c_str());
	
	command = "sudo -i tc qdisc del dev ";
	command += netcard;
	command += " root";
	system(command.c_str());
	printf("line 18\n");
	//system("firefox http://www.youtube.com/watch?v=un8FAjXWOBY");
	//printf("unblocked trafic\n");

	vector<UsageInfo> ipPorts = GetUsageInformation(netcard, 1000);
	printf("line 23\n");
	int size = ipPorts.size();
	int bwidth = maxBandwidth/size;
		
	double speed=0;
	for(int i = 0; i < size; i++)
	{
		speed+=ipPorts[i].Speed;
	}
	printf("maxBandwidth %.2f\n", speed);
	stringstream ss;
	ss << bwidth;
	string bandwidth = ss.str();
	ss.str("");
	if(minBandwidth<bwidth)
	{	

//		sprintf(command, "sudo -i tc qdisc del dev %s root", netcard);
		command = "sudo -i tc qdisc add dev ";
		command += netcard;
		command += " root handle 1:0 htb default 9999";
		//sprintf(command, "sudo -i tc qdisc add dev %s root handle 1:0 htb default %i", netcard, 9999);
		system(command.c_str());
		
		for(int l = 10; l < size+10; l++)//size+10
		{
			int tempP = ipPorts[l-10].Port;
			//const char* tempIP = ipPorts[l-10].IP.c_str();
			printf("Port %i, IP %s, Bandwidth %s\n", tempP, ipPorts[l-10].IP.c_str(), bandwidth.c_str());
			ss.str("");
			ss << l;
			string k = ss.str();
			command = " sudo -i tc class add dev ";
			command += netcard;
			command += " parent 1:0 classid 1:";
			command += k;
			command += " htb rate ";
			command += bandwidth;
			command += "kbps ceil ";
			command += bandwidth;
			command += "kbps prio 0";
			//sprintf(command, "sudo -i tc class add dev %s parent 1:0 classid 1:%i htb rate %ikbps ceil %ikbps prio 0", netcard, k, bandwidth, bandwidth);
			system(command.c_str());
			command = "sudo -i iptables -t mangle -A INPUT -i ";
			command += netcard;
			command += " -s ";
			command += ipPorts[l-10].IP;
			command += " -p tcp --sport ";
			ss.str("");
			ss<<ipPorts[l-10].Port;
			command +=  ss.str();
			command += " -j CONNMARK --set-mark ";
			command += k;
			//sprintf(command, "sudo -i iptables -t mangle -A INPUT -i %s -s %s -p tcp --sport %i -j CONNMARK --set-mark %i", netcard, ipPorts[k-10].ip, ipPorts[k-10].port, k);
			system(command.c_str());
			command = "sudo -i tc filter add dev ";
			command += netcard;
			command += " parent 1:0 prio 0 protocol ip handle ";
			command += k;
			command += " fw flowid 1:";
			command += k;
			//sprintf(command, "sudo -i tc filter add dev %s parent 1:0 prio 0 protocol ip handle %i fw flowid 1:%i", netcard, k, k);
			system(command.c_str());
		}
	}
}

void blockAll()
{
	string command = "iptables -P INPUT DROP";
	system(command.c_str());
	//printf("blocked traffic\n");
	//system("firefox http://www.youtube.com/watch?v=un8FAjXWOBY");
	/*int max = 65535;
	string command = "sudo -i tc qdisc del dev ";
	command += netcard;
	command += " root";
	system(command.c_str());
	
	command = "sudo -i tc qdisc add dev ";
	command += netcard;
	command += " root handle 1:0 htb default 9999";
	system(command.c_str());

	stringstream ss;
	//for(int l=0+10;l<max+10;l++)
	//{
		//printf("%i\n", l);
		int l = 10;
		ss.str("");
		ss << l;
		string k = ss.str();
		command = " sudo -i tc class add dev ";
		command += netcard;
		command += " parent 1:0 classid 1:";
		command += k;
		command += " htb rate 1";
		command += "kbps ceil 1";
		command += "kbps prio 0";
		system(command.c_str());
		
		command = "iptables -A OUTPUT -t mangle -p tcp --sport 10:65535";
		command += " -j CONNMARK --set-mark ";
		command += k;
		system(command.c_str());
	//}*/
}

int main(int argc, char** argv)
{
	printf("hello world\n");
	myTrafficControl();
	blockAll();
	sleep(1);
	myTrafficControl();
	printf("END\n");
	return 0;
}




